# write by hasaki
# first edit on 2019/04/16
# last change on 2019/04/18
# 从交易所获取到行情数据，生成K线图,成交量直方图,以及保存成需要上传的json
import time
from datetime import datetime
import os
import sys
import json
sys.path.append(os.getcwd())

from hashakisea.ui.plotter.matplot import Matplot
from hashakisea.api.digital.gateio import GateIO
from hashakisea.engine.log.logSystem import Ding


def getKline():
    '''获取K线数据'''
    a=GateIO()
    target_symbol_list=['eos_usdt']
    oglc=[]
    time1=[]
    volume=[]
    for i in target_symbol_list:
        data_return_from_exchange=a.kLine(i,sec=60,hour=24)
        K_line_data=data_return_from_exchange['data']
        for j in K_line_data:
            volume.append(float(j[1]))
            time1.append(time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(int(j[0])/1000)))
            oglc.append((float(j[0])/1000,float(j[2]),float(j[3]),float(j[4]),float(j[5])))

    return time1,oglc,volume

def plot(time_,oglc,volume_):
    '''生成行情图 展示k线,time=[],OGLC=[[1,2,3,4],[1,2,3,4],...],更改成matplot打印之后，时间数据也在开高低收里面'''
    e=Matplot()
    e.savePath='C:/Users/cchyp/Desktop/hasaki/quote/last_day/'
    e.time=time_
    e.volume(volume_)
    e.kline(oglc)

def Kmsg(time_,oglc,volume_):
    '''根据K线数据提炼出，现价和过往最高价和最低价以及成交量信息，json'''
    ding=Ding()
    ding.setting('https://oapi.dingtalk.com/robot/send?access_token=1723b5ff0e5b5b5c522d520aaf79e747f990b598172e241c6f0ef3c693a6b14f')
    last_time=time_[-1]
    last_price=round(oglc[-1][-1]*6.8,4)
    ding.send('时间：{}  最近一小时EOS收盘价:{}'.format(last_time,last_price))

if __name__=='__main__':
    while 1:
        time_,oglc,volume_=getKline()
        #print(time_[0],time_[1],time_[-1])
        #plot(time_,oglc,volume_)
        print('生成了新的行情数据')
        Kmsg(time_,oglc,volume_)
        time.sleep(300)